package com.example.smartcity4.ui.home;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.smartcity4.MainActivity;
import com.example.smartcity4.R;
import com.example.smartcity4.adapter.ListAdapter;
import com.example.smartcity4.adapter.NesHotAdapter;
import com.example.smartcity4.adapter.ServiceAdapter;
import com.example.smartcity4.javaBean.GsonBean;
import com.example.smartcity4.reteofi.SmartService;
import com.example.smartcity4.ui.metro.MetroActivity;
import com.google.gson.Gson;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;


public class HomeFragment extends Fragment {
    private View view;
    private TextView title;
    private ImageView left;
    private SearchView serch;
    private Banner banner;
    private GridView grid1;
    private GridView grid2;
    private GridView grid3;
    private ListView list;
    private List<String> advPic;
    private int flat = 0;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_home, container, false);
        initView();
        return view;
    }

    private void initView() {


        title = view.findViewById(R.id.title);
        left = view.findViewById(R.id.left);
        serch = view.findViewById(R.id.serch);
        banner = view.findViewById(R.id.banner);
        grid1 = view.findViewById(R.id.grid1);
        grid2 = view.findViewById(R.id.grid2);
        grid3 = view.findViewById(R.id.grid3);
        list = view.findViewById(R.id.list);
        title.setText("主页");
        initData();

    }

    private void initData() {
        get_banner(2);
        advPic = new ArrayList<>();
    }

    private void get_banner(final int type) {
        Retrofit retrofit = new Retrofit.Builder().baseUrl(MainActivity.host)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        SmartService smartService = retrofit.create(SmartService.class);
        Call<GsonBean> call = smartService.get_banner(String.valueOf(type));
        call.enqueue(new Callback<GsonBean>() {
            @Override
            public void onResponse(Call<GsonBean> call, Response<GsonBean> response) {
                for (int i = 0; i < response.body().rows.size(); i++) {
                    String pic = MainActivity.host + response.body().rows.get(i).advImg;
                    advPic.add(pic);
                }
                set_banner();
                set_service();
                set_Hot();
                set_cat();
                set_list("");

            }

            private void set_list(String type) {
                Retrofit retrofit = new Retrofit.Builder().baseUrl(MainActivity.host)
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();
                SmartService service = retrofit.create(SmartService.class);

                Call<GsonBean> call = service.get_list(type);
                call.enqueue(new Callback<GsonBean>() {
                    @Override
                    public void onResponse(Call<GsonBean> call, Response<GsonBean> response) {
                        ListAdapter listAdapter=new ListAdapter(response.body().rows,getContext());
                        list.setAdapter(listAdapter);
                        ViewGroup.LayoutParams layoutParams=list.getLayoutParams();
                        layoutParams.height=320*response.body().rows.size();
                        list.setLayoutParams(layoutParams);
                    }

                    @Override
                    public void onFailure(Call<GsonBean> call, Throwable throwable) {

                    }
                });

            }

            private void set_cat() {
                Retrofit retrofit = new Retrofit.Builder().baseUrl(MainActivity.host)
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();
                SmartService service = retrofit.create(SmartService.class);

                Call<GsonBean> call = service.get_cat();
                call.enqueue(new Callback<GsonBean>() {
                    @Override
                    public void onResponse(Call<GsonBean> call, final Response<GsonBean> response) {
                        final CatAdapter catAdapter = new CatAdapter(response.body().data, getContext());
                        grid3.setAdapter(catAdapter);
                        ViewGroup.LayoutParams layoutParams = grid3.getLayoutParams();
                        grid3.setNumColumns(response.body().data.size());
                        layoutParams.width = 250 * response.body().data.size();
                        grid3.setLayoutParams(layoutParams);
                        grid3.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                flat=position;
                                catAdapter.notifyDataSetChanged();
                                set_list(String.valueOf(response.body().data.get(position).id));
                            }
                        });
                    }

                    @Override
                    public void onFailure(Call<GsonBean> call, Throwable throwable) {

                    }
                });


            }


            private void set_service() {
                Retrofit retrofit = new Retrofit.Builder().baseUrl(MainActivity.host)
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();
                SmartService service = retrofit.create(SmartService.class);

                Call<GsonBean> call = service.get_service();
                call.enqueue(new Callback<GsonBean>() {
                    @Override
                    public void onResponse(Call<GsonBean> call, Response<GsonBean> response) {
                        grid1.setAdapter(new ServiceAdapter(response.body().rows, getContext()));
                        grid1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                if (position==3){
                                    startActivity(new Intent(getContext(), MetroActivity.class));
                                }else {
                                    Toast.makeText(getContext(),"功能暂未开发", Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                    }

                    @Override
                    public void onFailure(Call<GsonBean> call, Throwable throwable) {

                    }
                });

            }

            private void set_Hot() {
                Retrofit retrofit = new Retrofit.Builder().baseUrl(MainActivity.host)
                        .addConverterFactory(GsonConverterFactory.create())
                        .build();
                SmartService service = retrofit.create(SmartService.class);

                Call<GsonBean> call = service.get_hot("Y");
                call.enqueue(new Callback<GsonBean>() {
                    @Override
                    public void onResponse(Call<GsonBean> call, Response<GsonBean> response) {
                        grid2.setAdapter(new NesHotAdapter(response.body().rows, getContext()));
                    }

                    @Override
                    public void onFailure(Call<GsonBean> call, Throwable throwable) {

                    }
                });

            }

            @Override
            public void onFailure(Call<GsonBean> call, Throwable throwable) {

            }
        });
    }

    private void set_banner() {
        banner.setImageLoader(new GlideImageLoader());
        banner.setImages(advPic);
        banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int i) {
                Toast.makeText(getContext(), "点击了第" + i + "张图片", Toast.LENGTH_SHORT).show();
            }
        }).start();

    }

    public class GlideImageLoader extends ImageLoader {

        @Override
        public void displayImage(Context context, Object o, ImageView imageView) {
            Glide.with(context).load((String) o).into(imageView);
        }
    }

    public class CatAdapter extends BaseAdapter {
        private ArrayList<GsonBean.Data> data;
        private Context context;

        public CatAdapter(ArrayList<GsonBean.Data> data, Context context) {
            this.data = data;
            this.context = context;
        }

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = LayoutInflater.from(context).inflate(R.layout.cat_item, parent, false);
            TextView textView = convertView.findViewById(R.id.text);
            textView.setText(data.get(position).name);
            if (flat == position) {
                textView.setTextColor(Color.RED);
            } else {
                textView.setTextColor(Color.BLACK);
            }
            return convertView;
        }
    }
}